<?php
namespace Admin\Controller;
use Think\Controller;

/**
 * 总后台登陆
 */
class HomeController extends Controller {
    
    function __construct() {
		parent::__construct();
		$admin_id = session('admin_id');
		if (empty($admin_id)) {
			$url = \Org\Net\Fun::url('admin/index/index');
			header("Location:" . $url);
			exit;
		}
	}
    
    
    //登陆界面
    public function index(){
        //\Org\Net\Fun::url();
        $this->display();
    }
	
	
	/**
	 * 兑换码
	 */
	public function exchange(){
		//echo \Org\Net\Fun::randNumber();exit;
		//header("Content-type: text/html; charset=utf-8");
		$arr = explode("\r\n", $test);
		if($_FILES['csv']){			
			$exchangeModel = M('exchange');
			$filesName = $_FILES['csv'];
			$getData = array();
			$file = fopen($filesName['tmp_name'],'r');
			
			while ($data = fgetcsv($file)) { //每次读取CSV里面的一行内容
				for($i=0;$i<count($data);$i++){
					$data[$i] = iconv('GB2312', 'UTF-8', $data[$i]);
				}
				$getData[] = $data;
			}
            unset($getData[0]);
            unset($getData[1]);
			foreach($getData as $key=>$item){
                if($item[1] && $item[2] && $item[3]){
                    $saveData = array();
                    $saveData['city'] = $item[1];
                    $saveData['area'] = $item[2];
                    $saveData['money_changer'] = $item[3];
                    $saveData['address'] = $item[4];
                    $saveData['contacts'] = $item[5];
                    $arrCc = explode("|",  $item[6]);
                    $saveData['contacts_phone'] = serialize($arrCc);
                    $saveData['person_in_charge'] = $item[7];
                    $saveData['exchange_code'] = "";
                    $saveData['city_cd'] = $item[8];
                    $saveData['area_cd'] = $item[9];
                    $saveData['changer_cd'] = $item[10];
                    $saveData['add_time'] = time();
                    $exchangeModel->add($saveData);
                }
			}
            $returnData = array();
            $returnData['message'] = '操作成功';
            $returnData['href'] = \Org\Net\Fun::url('admin/home/exchange');
			echo json_encode($returnData);exit;
		}		
		$this->display('exchange');
	}
    
    /**
     * 兑换列表
     */
    public function exchangeList(){
        $keyword = I("get.keyword");
        $where = array();
        $where[] = "1";
        $whereString = "";
        if ($keyword) {
            $where[] = "(money_changer='{$keyword}' OR address like '%{$keyword}%')";
        }
        $whereString = implode(" AND ", $where);
        $exchangeModel = M('exchange'); // 实例化User对象
        $count = $exchangeModel->where($whereString)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        $list = $exchangeModel->where($where)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('count', $count); // 赋值分页输出
        $this->display('exchangeList');
    }
    
    /**
     * 生成兑换码
     */
    public function creatExchange(){
        $exchangeModel = M('exchange');
        $data = $exchangeModel->select();
        $returnData = array();
        $exchange_code = M('exchange_code');
        $oneData = $exchange_code->find();
        if($oneData){
            $returnData['message'] = '你已经生成过了';
            echo json_encode($returnData);exit;
        } else {
            $inserData = array();
            foreach($data as $key=>$item){
                for($i=1;$i<=100;$i++){
                    $arr = array();
                    $arr['exchange_id'] = $item['id'];
                    $arr['code'] = $item['city_cd'].$item['area_cd'].$item['changer_cd'].\Org\Net\Fun::randNumber();
                    $arr['effective'] = 0;
                    $arr['address'] = $item['address'];
                    $arr['add_time'] = time();
                    $inserData[] = $arr;
                }
            }
            $exchange_code->addAll($inserData);
            
            $returnData = array();
            $returnData['message'] = '生成成功';
            echo json_encode($returnData);exit;
        }
    }
    
    
    //兑换码列表
    public function exchange_code(){
        
        $id = I("get.id");
        $where = array();
        if($id>0){
            $where['exchange_id'] = $id;
        }
        $Exchange_code = M('Exchange_code'); // 实例化User对象
        $count = $Exchange_code->where($where)->count(); // 查询满足要求的总记录数
        $Page = new \Think\Page($count, 10); // 实例化分页类 传入总记录数和每页显示的记录数(25)
        $show = $Page->show(); // 分页显示输出
        $list = $Exchange_code->where($where)->select();
        $this->assign('list', $list); // 赋值数据集
        $this->assign('page', $show); // 赋值分页输出
        $this->assign('count', $count); // 赋值分页输出
        
        $this->display('exchange_code');
    }







    /**
    *   将所有的兑换吗都设置成小写
    */
    public function updateCodeSmall(){
        set_time_limit(0);
        $exchange_code = M('Exchange_code'); //兑换码对象
        $codeList = $exchange_code->where(array('effective'=>0))->select();
        //print_r($codeList);exit;

        foreach ($codeList as $key => $value) {
            $newCode = strtolower($value['code']);
            $updateCodeData = array();
            $updateCodeData['code'] = $newCode;
            $exchange_code->where("id=".$value['id'])->save($updateCodeData);
        }
        echo 'Is ok';exit;
    }


}